home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue53 / Clinic / DLLMsgTestForm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-12-03  |  1.4 KB  |  65 lines

  1. unit DLLMsgTestForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     edtNumber: TEdit;
  12.     btnCallDLL: TButton;
  13.     procedure btnCallDLLClick(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.   public
  17.     { Public declarations }
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.DFM}
  26. function DLLErrorMessage(DLL: HModule; ErrorCode: DWord): string;
  27. var
  28.   Len: Integer;
  29.   Buffer: array[0..255] of Char;
  30. begin
  31.   Len := FormatMessage(FORMAT_MESSAGE_FROM_HMODULE or
  32.     FORMAT_MESSAGE_ARGUMENT_ARRAY, Pointer(DLL), ErrorCode, 0, Buffer,
  33.     SizeOf(Buffer), nil);
  34.   while (Len > 0) and (Buffer[Len - 1] in [#0..#32, '.']) do Dec(Len);
  35.   SetString(Result, Buffer, Len);
  36. end;
  37.  
  38. procedure RaiseLastWin32DLLError(DLL: HModule);
  39. var
  40.   LastError: DWord;
  41.   Error: EWin32Error;
  42. begin
  43.   LastError := GetLastError;
  44.   Error := EWin32Error.Create(DLLErrorMessage(DLL, LastError));
  45.   Error.ErrorCode := LastError;
  46.   raise Error;
  47. end;
  48.  
  49. function Win32DLLCheck(DLL: HModule; RetVal: BOOL): BOOL;
  50. begin
  51.   if not RetVal then
  52.     RaiseLastWin32DLLError(DLL);
  53.   Result := RetVal;
  54. end;
  55.  
  56. function DoSomething(Value: Integer): Bool; stdcall;
  57. external 'TheDLL.DLL';
  58.  
  59. procedure TForm1.btnCallDLLClick(Sender: TObject);
  60. begin
  61.   Win32DLLCheck(GetModuleHandle('TheDLL.Dll'), DoSomething(StrToInt(edtNumber.Text)))
  62. end;
  63.  
  64. end.
  65.